﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class DateTime
    {
        /// <summary>
        /// 转换为时间格式，转换失败时：0 返回null, 1 返回当前时间
        /// </summary>
        /// <param name="default">转换失败时返回的值：0 返回null, 1 返回当前时间</param>
        public static System.DateTime? ToIsNULLDateTime1(this object self, int def = 0)
        {
            System.DateTime? rst = null;
            if (self == null)
            {
                if (def == 0) return null;
                else return System.DateTime.Now;
            }
            try
            {
                rst = System.DateTime.Parse(self.ToString());
            }
            catch (Exception)
            {
                if (def == 0) return null;
                else return System.DateTime.Now;
            }
            return rst;
        }


        /// <summary>
        /// 转换为时间格式，转换失败时返回的值：1900-1-1
        /// </summary>
        /// <param name="default">转换失败时返回的值：1900-1-1</param>
        public static System.DateTime ToIsNULLDateTime2(this object self)
        {
            System.DateTime rst= System.DateTime.Parse("1900-1-1");
            if (self == null)
            {
                return rst;
            }
            try
            {
                rst = System.DateTime.Parse(self.ToString());
            }
            catch (Exception)
            {
                return rst;
            }
            return rst;
        }

       
    }
}
